package com.luomo.java.jvm;

import java.util.HashMap;
import java.util.WeakHashMap;

/**
 * @author LiuMei
 * @date 2021-04-15.
 */
public class WeakHashMapDemo {

    public static void main(String[] args) {
        myHashMap();
        System.out.println("=========");
        myWeakHashMap();
    }

    private static void myHashMap(){
        HashMap<Integer,String> map = new HashMap<>();
        Integer key = 127;
        String value = "hashmap";
        map.put(key,value);
        System.out.println(map);
        key=null;
        System.out.println(map);
        System.gc();
        System.out.println(map+"\t"+map.size());
    }

    private static void myWeakHashMap(){
        WeakHashMap<Integer,String> map = new WeakHashMap<>();
        Integer key = -129;
        String value = "hashmap";
        map.put(key,value);
        System.out.println(map);
        key=null;
        System.out.println(map);
        System.gc();
        System.out.println(map+"\t"+map.size());
    }

}
